<?php
namespace Admin\Controller;
use Think\Controller;
class OrderController extends CommonController {
	public function _initialize(){
		
		parent::check();
	}
	/*
	 * 商品管理页面(标题、类别、价格、封面图)
	 */
	public function add(){
		$id = I('get.id');
		
		$Type = M('Type');
		
		$typelist = $Type->select();
		if( $id !='' ){
			$Ware = M('Ware');
			$list = $Ware->where("id={$id}")->find();
		}
		
		$this->assign('id',$id);
		$this->assign('typelist',$typelist);
		$this->assign('list',$list);
		$this->display();
	}
	/*
	 * 商品管理列表
	 */
    public function olist(){
		$id		= I('get.id');
		$act	= I('get.act');
		
		$Pay = M('Ware_pay');
		$User = M('User');
		
		$list = $Pay->order('status asc')->select();//查询
		$data = array();
		if( $list == false){
			$this->error($Ware->getError());
		}
		else{
			foreach( $list as $vo){
				$user_id = $vo['user_id'];
				$res = $User->where("id={$user_id}")->find();
				$user_name = $res['email'];
				$vo['user_name'] = $user_name;
				$data[] = $vo;
			}
		}
		
		$this->assign('data',$data);
		$this->display();
	}
	/*
	 * 商品详情添加/编辑页面
	 */
	public function intro(){
		$id = I('get.id');
		
		$Intro = M('Ware_intro');
		$res = $Intro->where("ware_id={$id}")->find();
		
		$this->assign('id',$id);
		$this->assign('res',$res);
		$this->display();
	}
	/*
	 * 商品规格添加/编辑页面
	 */
	public function cart(){
		$id = I('get.id');
		
		$this->assign('id',$id);
		$this->display();
	}
	/*
	 * 商品规格添加/编辑
	 */
	public function cartinsert(){
		$id			= I('get.id');
		$ware_cart	= I('post.ware_cart');
		$cart_list	= I('post.cart_list');
		
		$Cart = M('Ware_cart');
		$res = $Cart->create();
		if( $res == false){
			$this->error($Cart->getError());
		}
		else{
			$data = array(
				'ware_id'	=>	$id,
				'ware_cart'	=>	$ware_cart,
				'cart_list'	=>	$cart_list
			);
			$res = $Cart->add($data);
			if( $res == false){
				$this->error('添加失败');
			}
			$this->success('添加成功');
		}
	}
	
	/*
	 * 商品管理添加/编辑(标题、类别、价格、封面图)
	 */
	public function insert(){
		$id			= I('get.id');
		$name		= I('post.ware_name');
		$typenum	= I('post.typenum');
		$price		= I('post.price');
		
		if( $name =='' ){
			$this->error('任一选项不能为空');
		}
	
		$Ware = M('Ware');
		$res = $Ware->create();
		if( $res == false){
			$this->error($Ware->gerError());
		}
		else{
			$res['ware_name']	=$name;
			$res['typenum']		=$typenum;
			$res['addtime']		=time();
			$res['price']		=$price;
			if($_FILES['img']['name']!=''){
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize   =     2*1024*1024 ;// 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$path = realpath(__ROOT__);
				$upload->rootPath  = $path.'/Public/uploadfiles/'; // 设置附件上传根目录
				$upload->savePath  =     ''; // 设置附件上传（子）目录
				// 上传文件 
				$info   =   $upload->upload();
				if(!$info) {// 上传错误提示错误信息
					$this->error($upload->getError());
				}else{// 上传成功
					$img_src="/Public/uploadfiles/{$info['img']['savepath']}{$info['img']['savename']}";
					$res['head_img']	=$img_src;
					
				}	
				
			}	
			if( $id == ''){
				$Ware->add($res);
				$this->success('上传成功');
			}
			else{
				$Ware->where("id={$id}")->save($res);
				$this->success('修改成功');
			}		
		}
    }
	/*
	 * 商品详情添加/编辑(标题、类别、价格、封面图)
	 */
	public function itoinsert(){
		$id 			= I('get.id');
		$name	= I('post.name');
		$content	= I('post.content', '', '');
		
		/*if( $name == '' ){
			$this->error('有选项没填写');
		}*/
		echo $name;
		
		$Intro = M('Ware_intro');
		$res = $Intro->create();
		if( $res == false){
			$this->error($Intro->getError());
		}
		else{
			$ret = $Intro->where("ware_id={$id}")->find();
			if( $ret == false){
				$data = array(
					'ware_id'	=>	$id,
					'name'		=>	$name,
					'content'	=>	$content,
					'addtime'	=>	time()	
				);
				$res = $Intro->add($data);
				if( $res == false){
					$this->error('添加失败');
				}
				$this->success('添加成功');
			}
			else{
				$data = array(
					'name'		=>	$name,
					'content'	=>	$content,
					'addtime'	=>	time()	
				);
				$res = $Intro->where("ware_id={$id}")->save($data);
				if( $res == false){
					$this->error('修改失败');
				}
				$this->success('修改成功');
			}
		}
	}
	/*
	 * 图片上传
	 */
	public function imgUpload(){
		$id			= I('get.id');
		
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     2*1024*1024 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$path = realpath(__ROOT__);
		$upload->rootPath  = $path.'/Public/uploadfiles/'; // 设置附件上传根目录
		$upload->savePath  =     ''; // 设置附件上传（子）目录
		// 上传文件 
		$info   =   $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{// 上传成功
			$img_src="/Public/uploadfiles/{$info['_img']['savepath']}{$info['_img']['savename']}";
			$min_src="/Public/uploadfiles/{$info['_img']['savepath']}min_{$info['_img']['savename']}";
			$thumbpath="{$path}/Public/uploadfiles/{$info['_img']['savepath']}/";
			
			$image = new \Think\Image(); 
			$image->open("{$path}{$img_src}");
			$width = $image->width(); // 返回图片的宽度
			$height = $image->height(); // 返回图片的高度
			// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
			$image->thumb($width, $height)->save($thumbpath.$info['_img']['savename']);
			$image->thumb(58, 58)->save($thumbpath.'min_'.$info['_img']['savename']);
			
			$res['ware_id']=$id;
			$res['img_src']=$img_src;
			$res['min_src']=$min_src;
			$res['addtime']=time();
			$Photo=M('Ware_photo');
			$Photo->add($res);
			echo $img_src;
		}				
	}
}
